/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {

    //上传身份证正面
    $('.upload-identity-front').ajaxUpload({
        url: '/v1/upload',
        name:'identity-front',
        top: '190px',
        onSubmit:function (data){
            $('#text-identity-front').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-identity-front>img').attr('src','/pictures/' + info.savename);
                $('.input-identity-front').val(info.savename);
                $('#text-identity-front').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //上传身份证背面
    $('.upload-identity-back').ajaxUpload({
        url: '/v1/upload',
        name:'identity-back',
        top: '190px',
        onSubmit:function (data){
            $('#text-identity-back').html('正在上传中...');
            return true;
        },
        onComplete:function (json){
            if(json.status==200){
                var info = json.body.info[0];
                $('.upload-identity-back>img').attr('src','/pictures/' + info.savename);
                $('.input-identity-back').val(info.savename);
                $('#text-identity-back').html('上传图片');
            }else{
                BootstrapDialog.show({
                    title: '温馨提示',
                    message: '<div class="empty">'+ json.message +'</div>',
                    cssClass: 'modal-lol-ui'
                });
            }
        }
    });

    //--------------------------------------------------------------------------------

    loadList('/v1/teacher/contract/list.json');

    $('#view-contract-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var process = button.data('process') + 1;
        $('.contract-template').attr('href', '/template/'+id+'.html');
        $('.contract-id').val(id);
        $('.contract-process').val(process);
        $('.contract-step li').removeClass('active');
        $('.contract-step li').each(function(n){
            if (process > n) {
                $(this).addClass("active");
            }
        });
        if (process == 1) {
            $('.upload-identity').removeClass('hidden');
        }
        if (process == 2) {
            $('.wait-confirm').removeClass('hidden');
            $('.modal-footer .btn-save').addClass('hidden');
        }
        if (process == 3) {
            $('.pay-deposit').removeClass('hidden');
        }
        if (process == 4) {
            $('.modal-footer .btn-save').addClass('hidden');
        }
        if (process == 5) {
            $('.contract-attachment-info').removeClass('hidden');
            $('.modal-footer .btn-save').addClass('hidden');
        }
        var that = $(this);
        sendAjax('/v1/teacher/contract/detail.json', 'get', {id:id}, function(json){
            var contract = json.body.data;
            that.find('.contract-deposit').val(contract.deposit);
            that.find('.contract-sn').text(contract.sn);
            that.find('.contract-apart').text(contract.apart_name);
            that.find('.contract-deposit').text(contract.deposit);
            that.find('.contract-order-sn').text(contract.pay_order);
            that.find('.start-end-time').text(contract.start_at+' ~ '+contract.end_at);
            that.find('.contract-remark').text(contract.remark);
            if (process == 3) {
                var fee = that.find('.contract-deposit').val();
                pay('/v1/teacher/create/order', 'post', {fee:fee, tag:'deposit'}, function(json){
                    if (json && json.ret_content) {
                        if (json.ret_code == "0") {
                            $("#qrcode canvas").remove();
                            $("#qrcode").qrcode({
                                render: "canvas", //table方式
                                width: 100, //宽度
                                height: 100, //高度
                                text: json.ret_content.url //任意内容
                            });
                            $('.contract-order').val(json.ret_content.out_trade_no);
                            getResult(json.ret_content.out_trade_no);
                        }
                        else {
                            alert(json.ret_content);
                        }
                    }
                });
            }
            if (process == 5) {
                var attachment = JSON.parse(contract.attachment);
                $('.contract-attachment-info a').attr('href', '/files/'+attachment.contract_file);
                $('.contract-attachment-info').removeClass('hidden');
            }
        });
    });

    $('#view-contract-dialog').on('hide.bs.modal', function(){
        $('.contract-template').attr('href', '');
        $('.contract-id').val('');
        var process = $('.contract-process').val();
        if (process == 2) {
            $('.upload-img-input').val('');
            $('.img-identity-front').attr('src', '/images/front.png');
            $('.img-identity-back').attr('src', '/images/front.png');
        }
        if (process == 4) {
            $(this).find('.contract-deposit').val('');
        }
        $('.contract-process').val('');
    });

    $('#view-contract-dialog .btn-save').on('click', function(){
        var data = {
            id: $('.contract-id').val(),
            process: $('.contract-process').val()
        };
        if (data.process == 1) {

            data.attachment = JSON.stringify({"identity_front":$('.input-identity-front').val(), "identity_back":$('.input-identity-back').val()});
        }
        if (data.process == 3) {
            data.pay_order = $('.contract-pay-order').val();
        }
        if (data.process < 5) {
            sendAjax('/v1/teacher/change/contract/process', 'post', {data:data}, function(json){
                $('#view-contract-dialog').modal('hide');
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        } else {
            $('#view-contract-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text('合同已完成');
            $('#action-tip').modal('show');
        }
    });

    $('#edit-retire-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        sendAjax('/v1/teacher/contract/detail.json', 'get', {id:id}, function(json){
            var contract = json.body.data;
            var html = '';
            if (contract) {
                html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3">当前租房</label>';
                html += '<div class="col-sm-9">';
                html += '<input type="hidden" class="contract-id" value="'+contract.id+'" />';
                html += '<input type="hidden" class="apart-id" value="'+contract.apart_id+'" />';
                html += '<span>'+contract.apart_name+'</span>';
                html += '</div>';
                html += '</div>';
                html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3">退房说明</label>';
                html += '<div class="col-sm-9">';
                html += '<textarea class="form-control retire-remark"></textarea>';
                html += '</div>';
                html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3"></label>';
                html += '<div class="col-sm-9">';
                html += '<span>您确认要退房吗?</span>';
                html += '</div>';
                html += '</div>';
            } else {
                html += '<div class="form-group">';
                html += '<label class="control-label col-sm-3"></label>';
                html += '<div class="col-sm-9">';
                html += '<span>您暂时没有租房记录吗?</span>';
                html += '</div>';
                html += '</div>';
                $('#edit-retire-dialog').find('.btn-save').addClass('hidden');
            }
            $('#edit-retire-dialog').find('.form-horizontal').append(html);
        });
    });
    $('#edit-retire-dialog').on('hide.bs.modal', function(){
        $(this).find('.form-horizontal').html('');
        $(this).find('.btn-save').removeClass('hidden');

    });
    $('#edit-retire-dialog .btn-save').on('click', function(){
        var data = {
            contract_id: $('#edit-retire-dialog').find('.contract-id').val(),
            apart_id: $('#edit-retire-dialog').find('.apart-id').val(),

            remark: $('.retire-remark').val()
        };
        sendAjax('/v1/teacher/create/retire', 'post', {data:data}, function(json){
            $('#edit-retire-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });

    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url) {
    $("#contract-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'sn',
            title: '合同号'
        }, {
            field: 'start_end_time',
            title: '起止时间'
        }, {
            field: 'order_at',
            title: '签订时间'
        }, {
            field: 'process',
            title: '进度',
            formatter: function (value) {
                switch (value) {
                    case 0 : return '创建';
                    case 1 : return '身份认证';
                    case 2 : return '身份审核';
                    case 3 : return '支付押金';
                    case 4 : return '完成';
                    default: return '创建';
                }
            }
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-contract" data-toggle="modal" data-target="#view-contract-dialog" data-id="'+row.id+'" data-process="'+row.process+'">详情</a>';
                if (row.process == 4) action += '<a class="btn btn-xs btn-danger edit-retire" data-toggle="modal" data-target="#edit-retire-dialog" data-id="'+row.id+'">退房</a>';
                return action;
            }
        }]
    });
}

function getResult(order_id) {
    var interval = setInterval(function () {
        pay('/v1/teacher/select/order.json', 'get', {order_id: order_id, tag:'deposit'}, function(json){
            if (json && json.ret_content) {
                if (json.ret_code == "0") {
                    if(json.ret_content.status == 2){
                        clearInterval(interval);
                        $('.contract-order').val('');
                        $('.contract-pay-order').val(order_id);
                        alert("订单支付成功");
                    }
                }
            }
        });
    },5000);
}